package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aliyuncs.utils.StringUtils;

import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.UserHolder;
import com.tanhua.dubbo.api.PeachblossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.db.UserInfo;
import com.tanhua.model.domain.mongodb.Sounds;
import com.tanhua.model.vo.SoundVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;

@Service
public class PeachblossomService {
    @Autowired
    private OssTemplate ossTemplate;
    @DubboReference
    private PeachblossomApi peachblossomApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 桃花传音-发送语音
     *
     * @param soundFile 语音文件
     * @return
     */
    public void sendSounds(MultipartFile soundFile) {
        try {
            //获取文件的名称
            String originalFilename = soundFile.getOriginalFilename();
            //获取文件存储在oss上面的路径
            String soundUrl = ossTemplate.upload(originalFilename, soundFile.getInputStream());
            //创建sound对象
            Sounds sounds = new Sounds();
            //发布者
            sounds.setUserId(UserHolder.getUserId());
            //发布时间
            sounds.setCreated(System.currentTimeMillis());
            //文件的URL路径
            sounds.setSoundUrl(soundUrl);
            //保存文件
            peachblossomApi.save(sounds);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @DubboReference
    private UserInfoApi userInfoApi;


    public SoundVo getSounds() {
        //获取当前用户id
        long userId = UserHolder.getUserId();
        //去redis里面查询用户接收语音的次数
        String redisKey = "SOUNDTIMES" + userId;

        String times = redisTemplate.opsForValue().get(redisKey);
        //如果是空的话,那么就就存一个
        if (StringUtils.isEmpty(times)) {
            //首先获取今天最大的毫秒值
            String today = DateUtil.today();
            long todayMaxTime = DateUtil.parseDateTime(today + " 23:59:59").getTime();
            //获取现在的毫秒值
            long now = System.currentTimeMillis();
            //获取中间的差值
            long time = todayMaxTime - now;
            redisTemplate.opsForValue().set(redisKey, "10", Duration.ofMillis(time));
        }
        //查询相关信息
        Sounds sounds = peachblossomApi.randomSound(userId);
        SoundVo soundVo = new SoundVo();
        if (!ObjectUtil.isEmpty(sounds)) {
            //首先获取redis里面的值
            String value = redisTemplate.opsForValue().get(redisKey);
            Integer time = Convert.toInt(value);
            if (time > 0) {
                UserInfo userMessage = userInfoApi.findUserInfoById(sounds.getUserId());
                BeanUtils.copyProperties(userMessage, soundVo);
                soundVo.setId(Convert.toInt(userMessage.getId()));
                soundVo.setSoundUrl(sounds.getSoundUrl());
                soundVo.setRemainingTimes(time - 1);
                //然后redis里面做自减
                redisTemplate.opsForValue().decrement(redisKey, 1l);
            }
        }
        return soundVo;
    }
}
